﻿using FAMailUtils.Data.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FAMailUtils.Data.DAO
{
    public class PaymentDAO
    {
        DbHelper db = new DbHelper();
        public object Payment(PaymentDTO dto)
        {
            string sql = @"INSERT INTO [tblPayment]
           ([Amount]
           ,[OrderInfo]
           ,[TicketNo]
           ,[MerchTxnRef]
           ,[AgainLink]
           ,[Title]
           ,[receiverId]
           ,[UserID]
           ,[RegisterID]
           ,[PayemntTime], PaymentAmount, ExchangeRate)
     VALUES
           (@Amount
           ,@OrderInfo
           ,@TicketNo
           ,@MerchTxnRef
           ,@AgainLink
           ,@Title
           ,@receiverId, @userid, @registerid, getdate(), @PaymentAmount, @ExchangeRate) select @@identity";
            object id = db.ExecuteScalar(sql, new object[]{
            "Amount",dto.Amount
            ,"OrderInfo",dto.OrderInfo
            ,"TicketNo",dto.TicketNo
            ,"MerchTxnRef",dto.MerchTxnRef
            ,"AgainLink",dto.AgainLink
            ,"Title",dto.Title
            ,"receiverId",dto.receiverId
            ,"RegisterID",dto.RegisterID
            ,"UserID",dto.UserID
            ,"PaymentAmount",dto.PaymentAmount
            ,"ExchangeRate",dto.ExchangeRate
            });
            return id;
        }
        public bool Confirm(PaymentDTO dto)
        {
            string sql = "Update tblPayment set TxnResponseCode=@TxnResponseCode, TxnResponseCodeText=@TxnResponseCodeText, ConfirmationTime=getdate() where PaymentId=@PaymentId";
            int i = db.ExecuteNoneQuery(sql, new object[]{
            "PaymentID",dto.PaymentID
            ,"TxnResponseCode",dto.TxnResponseCode
            ,"TxnResponseCodeText",dto.TxnResponseCodeText
            });
            return i > 0;
        }
        public PaymentDTO GetByReceiverID(string receiverid)
        {
            PaymentDTO dto = new PaymentDTO();
            string sql = "select * from tblPayment where receiverid=@receiverid";
            return (PaymentDTO)db.GetObject(dto, sql, new object[] { "receiverid", receiverid });
        }

        public PaymentDTO GetByID(string p)
        {
            PaymentDTO dto = new PaymentDTO();
            string sql = "select * from tblPayment where paymentid=@paymentid";
            return (PaymentDTO)db.GetObject(dto, sql, new object[] { "paymentid", p });
        }
    }
}
